package com.sh.data.engine.application.service.datadev;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevNodeResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevOpeartionSummaryResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevOperationJobResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevTaskLogResponse;
import com.sh.data.engine.application.model.dto.workspace.UserResponse;
import com.sh.data.engine.application.model.mapper.datadev.OfflineDevOperationJobResponseMapper;
import com.sh.data.engine.application.model.mapper.datadev.OfflineDevOperationJobResponsePageMapper;
import com.sh.data.engine.application.model.request.datadev.OfflineDevOperationJobSearchRequest;
import com.sh.data.engine.application.model.request.datadev.OfflineDevTaskAlarmRequest;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.base.model.UserContext;
import com.sh.data.engine.domain.datadev.offline.model.domain.*;
import com.sh.data.engine.domain.datadev.offline.model.enums.OfflineDevTaskStopEnum;
import com.sh.data.engine.domain.datadev.offline.model.param.OfflineDevOperationJobSearchParam;
import com.sh.data.engine.domain.datadev.offline.model.param.OfflineDevTaskAlarmParam;
import com.sh.data.engine.domain.datadev.offline.model.param.OfflineDevTaskHistoryJobParam;
import com.sh.data.engine.domain.datadev.offline.service.DatadevJobService;
import com.sh.data.engine.domain.datadev.offline.service.OfflineDevMenuNodeService;
import com.sh.data.engine.domain.taskalarm.service.TaskAlarmService;
import com.sh.data.engine.domain.util.ConvertUtil;
import com.sh.data.engine.domain.workspace.manager.model.domain.UserDomain;
import com.sh.data.engine.domain.workspace.manager.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2024/7/12 18:08
 */
@Slf4j
@Service
public class OfflineDevOperationApplicationService {

    @Autowired
    private DatadevJobService datadevJobService;

    @Autowired
    private TaskAlarmService taskAlarmService;

    @Autowired
    private UserService userService;

    @Autowired
    private OfflineDevMenuNodeService offlineDevMenuNodeService;

    @Autowired
    private OfflineDevOperationJobResponseMapper offlineDevOperationJobResponseMapper;

    @Autowired
    private OfflineDevOperationJobResponsePageMapper offlineDevOperationJobResponsePageMapper;

    public OfflineDevOpeartionSummaryResponse getOfflineDevSummary(Long nodeId) {
        OfflineDevOperationSummaryDomain offlineDevOperationSummaryDomain =
            datadevJobService.getHistorySummary(nodeId);

        return ConvertUtil.copyProperties(
            offlineDevOperationSummaryDomain, OfflineDevOpeartionSummaryResponse.class);
    }

    public PagerResponse<OfflineDevOperationJobResponse> getOfflineDevOperationJobPage(
        OfflineDevOperationJobSearchRequest searchRequest) {
        Long projectId = ServletUtils.getProjectId();
        OfflineDevOperationJobSearchParam offlineDevOperationJobSearchParam =
            ConvertUtil.copyProperties(searchRequest, OfflineDevOperationJobSearchParam.class);
        offlineDevOperationJobSearchParam.setProjectId(projectId);
        PageResult<OfflineDevOperationJobDomain> pageResult =
            datadevJobService.getOfflineDevOperationJobPage(offlineDevOperationJobSearchParam);

        return offlineDevOperationJobResponsePageMapper.toPageResponse(pageResult);
    }

    public PagerResponse<OfflineDevOperationJobResponse> getOfflineDevJobHistoryPage(
        Long nodeId, Integer taskStatus, Integer execType,
        String order, String orderField, Integer pageNum, Integer pageSize) {
        Long projectId = ServletUtils.getProjectId();

        OfflineDevTaskHistoryJobParam offlineDevTaskHistoryJobParam =
            new OfflineDevTaskHistoryJobParam();
        offlineDevTaskHistoryJobParam.setProjectId(projectId);
        offlineDevTaskHistoryJobParam.setPageNum(pageNum);
        offlineDevTaskHistoryJobParam.setPageSize(pageSize);
        offlineDevTaskHistoryJobParam.setNodeId(nodeId);
        offlineDevTaskHistoryJobParam.setExecType(execType);
        offlineDevTaskHistoryJobParam.setTaskStatus(taskStatus);
        offlineDevTaskHistoryJobParam.setOrder(order);
        offlineDevTaskHistoryJobParam.setOrderField(orderField);

        PageResult<OfflineDevOperationJobDomain> pageResult =
            datadevJobService.getOfflineDevOperationHistoryJobPage(offlineDevTaskHistoryJobParam);

        return offlineDevOperationJobResponsePageMapper.toPageResponse(pageResult);
    }

    public List<OfflineDevOperationJobResponse> getOfflineDevOperationJobList(Long jobId) {
        List<OfflineDevOperationJobDomain> res =
            datadevJobService.getOfflineDevOperationJobList(jobId, null);
        return offlineDevOperationJobResponseMapper.map(res);
    }

    public OfflineDevTaskLogResponse taskLog(Long jobId, Integer offset, String rsHasShow) {
        OfflineDevTaskLogDomain taskLog = datadevJobService.getTaskLog(jobId, offset, rsHasShow);
        return ConvertUtil.copyProperties(taskLog, OfflineDevTaskLogResponse.class);
    }

    public Long taskRestart(Long id) {
        Long projectId = ServletUtils.getProjectId();
        String userId = UserContext.getUserId();
        Integer execType = 0;
        return datadevJobService.startTask(id, execType, projectId, userId);
    }

    public boolean taskStop(Long logId) {
        datadevJobService.stopJob(logId, OfflineDevTaskStopEnum.OPERATION_TASK.getCode());
        return true;
    }

    public OfflineDevOperationTaskflowDetailInfoDomain getOfflineDevTaskFlowDetailDomain(Long nodeId) {
        return datadevJobService.getOfflineDevTaskFlowDetailDomain(nodeId);
    }

    public Object getNodeDetail(Long nodeId) throws IOException {
        return datadevJobService.getNodeDetail(nodeId);
    }

    public boolean saveOfflineDevTaskAlarm(OfflineDevTaskAlarmRequest offlineDevTaskAlarmRequest) {
        Long projectId = ServletUtils.getProjectId();
        String userId = UserContext.getUserId();
        OfflineDevTaskAlarmParam offlineDevTaskAlarmParam =
            ConvertUtil.copyProperties(offlineDevTaskAlarmRequest, OfflineDevTaskAlarmParam.class);
        offlineDevTaskAlarmParam.setCreatorId(userId);
        offlineDevTaskAlarmParam.setProjectId(projectId);
        return taskAlarmService.saveTaskFlowAlarmConfig(offlineDevTaskAlarmParam);
    }

    public List<OfflineDevNodeResponse> getAllTaskFlowList() {
        Long projectId = ServletUtils.getProjectId();
        List<OfflineDevNodeDomain> offlineDevNodeDomains =
            offlineDevMenuNodeService.getAllTaskFlowList(projectId);
        return ConvertUtil.copyProperties(offlineDevNodeDomains, OfflineDevNodeResponse.class);
    }

    public List<UserResponse> getCallUserInfo() {
        List<UserDomain> allUserDomain = userService.findAll();
        return ConvertUtil.copyProperties(allUserDomain, UserResponse.class);
    }

    public Long getParentTypeByNodeId(Long nodeId) {
        return offlineDevMenuNodeService.getParentTypeByNodeId(nodeId);
    }
}
